﻿using System;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;
using iopt.solvency.pillarone.client.Web.Services;
using iopt.solvency.pillarone.client.Web;

namespace iopt.solvency.pillarone.client.Models
{

    public class WorkspaceControlModel : ComplexObject
    {
        public static readonly Uri azureuri = new Uri("http://localhost:49260/services/coredataservice.svc/");
        public static readonly Uri localeuri=  new Uri("http://localhost:49260/services/coredataservice.svc/");
        public static readonly bool azuremode = false;

        private CoreServices.DatabaseModelContainer client = null;
        private iopt.solvency.pillarone.client.Web.Services.CoreDomainContext domainclient;

        public WorkspaceControlModel()
        {
            //client = new CoreServices.DatabaseModelContainer(
            //    azuremode ? azureuri :
            //    localeuri);

            //client = new CoreServices.DatabaseModelContainer(new Uri("services/coredataservice.svc", UriKind.Relative));

            domainclient = new CoreDomainContext();

            
        }

        public ICommand CreateWorkspace { get { return new DelegateCommand(CreateWorkspaceHandler); } }
        public void CreateWorkspaceHandler()
        {

            //client.WorkspaceJeu.BeginExecute(
            //    (System.IAsyncResult ar) => 
            //        WorkspaceList = , null);


           //// client.WorkspaceJeu.be
            //WorkspaceList = client.WorkspaceJeu.AsEnumerable().ToArray();
           // var id = (from w in client.WorkspaceJeu select w.ID).AsQueryable().be.ToList().Max()+1;
            var wspace = new Workspace();//iopt.solvency.pillarone.client.Web.Workspace.CreateWorkspace(1, "Worspace_v2_" + 1);
            wspace.Name = "Worspace_v2_"+12500;
            wspace.ID = 12500;
            domainclient.Workspaces.Add(wspace);
            domainclient.SubmitChanges();

            //client
            //client.AddToWorkspaceJeu(wspace);
            //client.BeginSaveChanges(
            //    (System.IAsyncResult ar) => 
            //        WorkspaceList = client.WorkspaceJeu.AsEnumerable().ToArray(), null);
        }

        private  CoreServices.Workspace[] _WorkspaceList;
        public CoreServices.Workspace[] WorkspaceList
        {
            get
            {
                return _WorkspaceList;
            }
            set
            {
                _WorkspaceList = value;
                RaisePropertyChanged("WorkspaceList");
            }
        }

   
    }
}
